package com.giscloud.limp.service.impl;

import com.alibaba.fastjson.JSON;
import com.giscloud.commons.model.Result;
import com.giscloud.datasource.page.PageInfo;
import com.giscloud.limp.dao.CaseFirstInstanceDao;
import com.giscloud.limp.dao.CaseRetrialDao;
import com.giscloud.limp.dao.DefendantDao;
import com.giscloud.limp.dao.PlaintiffDao;
import com.giscloud.limp.entity.*;
import com.giscloud.limp.entity.dto.CaseRetrialDTO;
import com.giscloud.limp.service.CaseRetrialService;
import com.giscloud.limp.service.CaseWarnService;
import com.giscloud.token.utils.TokenUserUtil;
import com.github.pagehelper.PageHelper;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service("caseRetrialService")
@Log4j2
public class CaseRetrialServiceImpl implements CaseRetrialService {
    private final  CaseRetrialDao caseRetrialDao;
    @Autowired
    private TokenUserUtil tokenUserUtil;
    @Autowired
    public CaseRetrialServiceImpl(CaseRetrialDao caseRetrialDao) {
        this.caseRetrialDao = caseRetrialDao;
    }

    //被告
    @Autowired
    private DefendantDao defendantDao;

    //原告
    @Autowired
    private PlaintiffDao plaintiffDao;

    @Autowired
    private CaseFirstInstanceDao caseFirstInstanceDao;

    @Autowired
    private CaseWarnService caseWarnService;

    /**
    *
    * 列表
    */
    @Override
    public PageInfo queryPage(CaseRetrialDTO caseRetrialDTO) {
        log.debug("#queryPage parameter ={}", JSON.toJSONString(caseRetrialDTO));
        PageHelper.startPage(caseRetrialDTO.getPage(), caseRetrialDTO.getRows());
        CaseRetrialExample caseRetrialExample = new CaseRetrialExample();
        CaseRetrialExample.Criteria criteria = caseRetrialExample.createCriteria();
        List<CaseRetrial> caseRetrials = caseRetrialDao.selectByExample(caseRetrialExample);
        return PageInfo.of(caseRetrials);
    }


    /**
     * 信息
     */
    @Override
    public CaseRetrial getById(Integer caseRetrialId){
        log.debug("#getById parameter ={}", caseRetrialId);
        return caseRetrialDao.selectByPrimaryKey(caseRetrialId);
    }

    /**
     * 保存
     */
    @Override
    @Transactional
    public Result save(CaseRetrial caseRetrial){
        log.debug("#save parameter ={}", JSON.toJSONString(caseRetrial));
        caseRetrial.setCreatedStaffId(tokenUserUtil.getUserId().intValue());
        caseRetrialDao.insertSelective(caseRetrial);
        if(CollectionUtils.isNotEmpty(caseRetrial.getPlaintiffs())){
            for(Plaintiff plaintiff : caseRetrial.getPlaintiffs()){
                plaintiff.setLitigationStage(3);
                plaintiff.setCaseId(caseRetrial.getCaseId());
                plaintiffDao.insertSelective(plaintiff);
            }
        }
        if(CollectionUtils.isNotEmpty(caseRetrial.getDefendants())){
            for(Defendant defendant : caseRetrial.getDefendants()){
                defendant.setLitigationStage(3);
                defendant.setCaseId(caseRetrial.getCaseId());
                defendantDao.insertSelective(defendant);
            }
        }
        CaseWarn caseWarn = new CaseWarn();
        caseWarn.setCaseId(caseRetrial.getCaseId());
        caseWarn.setLitigationStage(3);
        caseWarn.setWarnDate(caseRetrial.getWarnDate());
        caseWarn.setWarnContent(caseRetrial.getWarnContent());
        caseWarnService.save(caseWarn);
        CaseFirstInstance caseFirstInstance = caseFirstInstanceDao.selectLitigationStage(caseRetrial.getCaseId());
        caseFirstInstance.setCaseId(caseRetrial.getCaseId());
        caseFirstInstanceDao.updateByPrimaryKeySelective(caseFirstInstance);
        return Result.success("保存成功");

    }

    /**
     * 修改
     */
    @Override
    @Transactional
    public Result updateById(CaseRetrial caseRetrial){
         log.debug("#updateById parameter ={}", JSON.toJSONString(caseRetrial));
        caseRetrial.setOperateStaffId(tokenUserUtil.getUserId().intValue());
        caseRetrial.setVersion(new Date());
         CaseRetrialExample caseRetrialExample = new CaseRetrialExample();
         CaseRetrialExample.Criteria criteria = caseRetrialExample.createCriteria();
         criteria.andCaseRetrialIdEqualTo(caseRetrial.getCaseRetrialId());
         caseRetrialDao.updateByExampleSelective(caseRetrial,caseRetrialExample);
        if(CollectionUtils.isNotEmpty(caseRetrial.getPlaintiffs())){
            PlaintiffExample plaintiffExample = new PlaintiffExample();
            plaintiffExample.createCriteria().andCaseIdEqualTo(caseRetrial.getCaseId()).andLitigationStageEqualTo(3);
            plaintiffDao.deleteByExample(plaintiffExample);
            for(Plaintiff plaintiff : caseRetrial.getPlaintiffs()){
                plaintiff.setLitigationStage(3);
                plaintiff.setCaseId(caseRetrial.getCaseId());
                plaintiffDao.insertSelective(plaintiff);
            }
        }
        if(CollectionUtils.isNotEmpty(caseRetrial.getDefendants())){
            DefendantExample defendantExample = new DefendantExample();
            defendantExample.createCriteria().andCaseIdEqualTo(caseRetrial.getCaseId()).andLitigationStageEqualTo(3);
            defendantDao.deleteByExample(defendantExample);
            for(Defendant defendant : caseRetrial.getDefendants()){
                defendant.setLitigationStage(3);
                defendant.setCaseId(caseRetrial.getCaseId());
                defendantDao.insertSelective(defendant);
            }
        }
        CaseWarn caseWarn = new CaseWarn();
        caseWarn.setCaseId(caseRetrial.getCaseId());
        caseWarn.setLitigationStage(3);
        caseWarn.setWarnDate(caseRetrial.getWarnDate());
        caseWarn.setWarnContent(caseRetrial.getWarnContent());
        caseWarnService.updateById(caseWarn);
        return Result.success("修改成功");
    }
    /**
     * 删除
     */
    @Override
    @Transactional
    public Result removeByIds(List<Integer> caseRetrialIds){
        log.debug("#removeByIds parameter ={}", JSON.toJSONString(caseRetrialIds));
        CaseRetrialExample caseRetrialExample = new CaseRetrialExample();
        caseRetrialExample.createCriteria().andCaseRetrialIdIn(caseRetrialIds);
        List<CaseRetrial> caseRetrials = caseRetrialDao.selectByExample(caseRetrialExample);
        if(CollectionUtils.isNotEmpty(caseRetrials)){
            Set<Integer> set = new HashSet<>();
            for(CaseRetrial caseRetrial : caseRetrials){
                set.add(caseRetrial.getCaseId());
            }
            PlaintiffExample plaintiffExample = new PlaintiffExample();
            plaintiffExample.createCriteria().andCaseIdIn(new ArrayList<>(set));
            plaintiffDao.deleteByExample(plaintiffExample);

            DefendantExample defendantExample = new DefendantExample();
            defendantExample.createCriteria().andCaseIdIn(new ArrayList<>(set));
            defendantDao.deleteByExample(defendantExample);
        }
        caseRetrialDao.deleteByExample(caseRetrialExample);
        if(CollectionUtils.isNotEmpty(caseRetrials)){
            for(CaseRetrial caseRetrial : caseRetrials){
                CaseFirstInstance caseFirstInstance = caseFirstInstanceDao.selectLitigationStage(caseRetrial.getCaseId());
                caseFirstInstance.setCaseId(caseRetrial.getCaseId());
                caseFirstInstanceDao.updateByPrimaryKeySelective(caseFirstInstance);
                CaseWarn caseWarn = new CaseWarn();
                caseWarn.setCaseId(caseRetrial.getCaseId());
                caseWarn.setLitigationStage(3);
                caseWarnService.remove(caseWarn);
            }
        }
        return Result.success("删除成功");
    }

}